package nsh.cl.nd.khtn.mazedash;

import nsh.cl.nd.khtn.mazedash.baseclasses.BaseFadeInScene;
import nsh.cl.nd.khtn.mazedash.game.menu.MenuScene;
import nsh.cl.nd.khtn.mazedash.game.play.PlayScene;

import org.andengine.engine.Engine;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.FontManager;
import org.andengine.opengl.font.StrokeFont;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.util.color.Color;

import android.content.res.AssetManager;

public class ScreenManager {

	private Engine engine;
	private StrokeFont gameFont;
	private StrokeFont bigFont;
	private StrokeFont smallFont;
	private StrokeFont instructionFont;

	private PlayScene playScene = new PlayScene();
	private MenuScene menuScene = new MenuScene();

	public ScreenManager(Engine mEngine) {
		engine = mEngine;
	}

	public Scene getCurrentScene() {
		if (engine != null) {
			if (engine.getScene().hasChildScene()) {
				return engine.getScene().getChildScene();
			}
			return engine.getScene();
		}
		return null;
	}

	public void loadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		FontFactory.setAssetBasePath("font/");
		RootGameActivity gameActivity = RootGameActivity.getInstance();
		ITexture fontTexture = new BitmapTextureAtlas(gameActivity.getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT, TextureOptions.BILINEAR);
		ITexture bigfontTexture = new BitmapTextureAtlas(gameActivity.getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT, TextureOptions.BILINEAR);
		ITexture smallfontTexture = new BitmapTextureAtlas(gameActivity.getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT, TextureOptions.BILINEAR);
		FontManager fontManager = gameActivity.getFontManager();
		AssetManager assets = gameActivity.getAssets();
		gameFont = FontFactory.createStrokeFromAsset(fontManager, fontTexture, assets, GameConstant.FONT_FILE,
				GameConstant.FONT_SIZE_NORMAL, true, Color.WHITE_ARGB_PACKED_INT, 2, Color.BLACK_ARGB_PACKED_INT);
		gameFont.load();
		bigFont = FontFactory.createStrokeFromAsset(fontManager, bigfontTexture, assets, GameConstant.FONT_FILE,
				GameConstant.FONT_SIZE_BIG, true, Color.WHITE_ARGB_PACKED_INT, 2, Color.BLACK_ARGB_PACKED_INT);
		bigFont.load();
		smallFont = FontFactory.createStrokeFromAsset(fontManager, smallfontTexture, assets, GameConstant.FONT_FILE,
				GameConstant.FONT_SIZE_SMALL, true, Color.WHITE_ARGB_PACKED_INT, 2, Color.BLACK_ARGB_PACKED_INT);
		smallFont.load();
		
		ITexture instructionFontTexture = new BitmapTextureAtlas(gameActivity.getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT, TextureOptions.BILINEAR);
		instructionFont = FontFactory.createStrokeFromAsset(fontManager, instructionFontTexture, assets, GameConstant.FONT2_FILE,
				50, true, Color.WHITE_ARGB_PACKED_INT, 1, Color.WHITE_ARGB_PACKED_INT);
		instructionFont.load();
		
		smallFont.prepareLetters(".0123456789abcdefghijklmnopqrstuvwxyz".toCharArray());
		bigFont.prepareLetters(".0123456789abcdefghijklmnopqrstuvwxyz".toCharArray());
		gameFont.prepareLetters(".0123456789abcdefghijklmnopqrstuvwxyz".toCharArray());
		instructionFont.prepareLetters(".0123456789abcdefghijklmnopqrstuvwxyz".toCharArray());
		
		// Load all scenes resources
		getPlayScene().loadResources();
		getMenuScene().loadResources();
	}

	public void showScene(BaseFadeInScene sceneToShow) {
		RootGameActivity.getInstance().getEngine().setScene(sceneToShow);
		sceneToShow.animateIn();
	}

	public Font getGameFont() {
		return gameFont;
	}

	public Font getBigFont() {
		return bigFont;
	}

	public Font getSmallFont() {
		return smallFont;
	}
	
	public PlayScene getPlayScene() {
		return playScene;
	}

	public StrokeFont getInstructionFont() {
		return instructionFont;
	}

	public void setInstructionFont(StrokeFont instructionFont) {
		this.instructionFont = instructionFont;
	}

	public MenuScene getMenuScene() {
		return menuScene;
	}

}